package com.example.administrator.officeplatform.base;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by Administrator on 2015/12/25.
 */
public abstract class BaseFragment extends Fragment {
    protected boolean isViewInitiated;
    protected boolean isVisibleToUser;
    protected boolean isDataInitiated;
    protected View mView;
    /**
     * 屏幕的宽度�?高度、密�?
     */


    public BaseFragment() {
        super();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mView=setContentView(inflater,container,savedInstanceState);
        initViews();
        initEvents();
        return mView;
    }



    protected abstract View setContentView(LayoutInflater inflater, ViewGroup container,
                                           Bundle savedInstanceState);
    protected abstract void initViews();
    protected abstract void initEvents();
    public View findViewById(int id) {
        return mView.findViewById(id);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewInitiated = true;
        prepareFetchData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        prepareFetchData();
    }

//    public abstract void fetchData();

    public boolean prepareFetchData() {
        return prepareFetchData(false);
    }

    public boolean prepareFetchData(boolean forceUpdate) {
//        if (isVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
        if (isVisibleToUser && isViewInitiated) {
            initData();
            isDataInitiated = true;
            return true;
        }
        return false;
    }
    protected abstract void initData();
    protected boolean isNull(EditText et) {
        boolean b = false;
        if (isNull(et.getText().toString())) {
            b = true;
        }
        return b;
    }
    protected boolean isNull(TextView tv) {
        boolean b = false;
        if (isNull(tv.getText().toString())) {
            b = true;
        }
        return b;
    }
    protected boolean isNull(String str) {
        boolean b = false;
        if (str.trim() == null
                || str.trim().length() == 0) {
            b = true;
        }
        return b;
    }
}
